//
//  NewsViewController.m
//  HuiWenMovie
//
//  Created by ios on 15/7/19.
//  Copyright (c) 2015年 ios. All rights reserved.
//

#import "NewsViewController.h"
#import "NewModel.h"
#import "NewsCell.h"
#import "UIViewExt.h"
#import "UIImageView+WebCache.h"
#import "ImageListViewController.h"
#define kwth [UIScreen mainScreen].bounds.size.width
#define kheight [UIScreen mainScreen].bounds.size.height

@interface NewsViewController ()
{
    UIImageView *poster;
    UILabel *posterLabel;
}

@end

@implementation NewsViewController

-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if(self)
    {
        self.title = @"新闻";
        
    }
    
    return self;
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self configureNewsTable];
    [self configurePoster];
    [self loadData];

    // Do any additional setup after loading the view.
}

- (void)configurePoster
{
    
    poster = [[UIImageView alloc]initWithFrame:CGRectMake(0, 64, kwth, 200)];
    posterLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 150+64, kwth , 50)];
    posterLabel.textColor = [UIColor whiteColor];
    posterLabel.textAlignment = NSTextAlignmentCenter;
    
    [self.view addSubview:poster];
    [self.view addSubview:posterLabel];

}
- (void)configureNewsTable
{
    UIView *headSection = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, 200)];
    _newsTableView = [[UITableView alloc]initWithFrame:self.view.frame];
    _newsTableView.dataSource = self;
    _newsTableView.delegate = self;
    _newsTableView.tableHeaderView = headSection;
    
    UINib *nib = [UINib nibWithNibName:@"NewsCell" bundle:[NSBundle mainBundle]];
    [_newsTableView registerNib:nib forCellReuseIdentifier:@"cell2"];
    
    [self.view addSubview:_newsTableView];

}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat offSetY = scrollView.contentOffset.y;
    
   // NSLog(@"%f",offSetY);
    
    if(offSetY > -64)//向上滑
    {
        poster.top = -offSetY;
        posterLabel.bottom = poster.bottom;
        
    }
    else             //向下滑
    {
        poster.height = ABS(offSetY)-64+200;
        poster.width = kwth/200 * poster.height;
        poster.left = (kwth-poster.width)/2;
        poster.top = 64;
        
        posterLabel.autoresizingMask = UIViewAutoresizingFlexibleTopMargin;
      //    posterLabel.height = ABS(offSetY)-64+50;
       // posterLabel.width = kwth/50 * posterLabel.height;
        //posterLabel.left = poster.left;
    
    }
    
    

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    return [_newsList count]-1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NewsCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell2"];
    cell.reportModel = _newsModels[indexPath.row];
   // cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    return cell;

}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 85;
}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ImageListViewController *imageList = [[ImageListViewController alloc]init];
    [self.navigationController pushViewController:imageList animated:YES];
    

}


- (void)loadData
{
    _newsList = [[NSArray alloc]init];
    _newsModels = [[NSMutableArray alloc]init];
    NSString *path = [[NSBundle mainBundle]pathForResource:@"news_list.json" ofType:nil];
    NSData *data = [NSData dataWithContentsOfFile:path];
    _newsList = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
    for (NSDictionary *dic in _newsList) {
        
        NewModel *model = [[NewModel alloc]init];
        model.title = [dic valueForKey:@"title"];
        model.imageName = [dic valueForKey:@"image"];
        NSNumber *temp =[dic valueForKey:@"type"];
        model.type = [temp integerValue];
        model.summary = [dic valueForKey:@"summary"];
        [_newsModels addObject:model];
    }
    [poster sd_setImageWithURL:[NSURL URLWithString:[_newsModels[0] imageName]]];
    posterLabel.text = [_newsModels[0] title];
    [_newsModels removeObjectAtIndex:0];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
